package com.air.common.util;

import java.net.URLEncoder;
import java.util.List;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.air.admin.mo.AdminPrivilege;
import com.air.admin.mo.AdminRole;
import com.air.admin.mo.BasicAdminInfo;
import com.air.admin.vo.AdminLoginRecorderVO;
import com.air.common.mo.BasicUserInfo;
import com.air.common.vo.UserLoginRecorderVO;






public class WebUtil {
	
	
	
	public static boolean isAnsyCalling(HttpServletRequest request){
		try{
			if(!(request.getHeader("accept").indexOf("application/json")>-1 || request.getHeader("X-Requested-With")!=null)){
				return false;
			}
		}catch (Exception e) {
			return true;
		}
		return true;
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static String parseRequestParamenter2String(HttpServletRequest request){
		
		Set<String> paraKeySet = request.getParameterMap().keySet();
		
		String parsedString = "";
		
		for(String aKey:paraKeySet){
			parsedString += aKey+"="+URLEncoder.encode(request.getParameter(aKey))+"&";
		}
		if (parsedString.length()>0){
			parsedString = parsedString.substring(0,parsedString.length()-1);
		}
		
		return parsedString;
	}
	
	
	
	public static Integer getUserIdFromCookie(HttpServletRequest request){
		
		
		boolean findUserProfileCookie=false;
		String  userProfileCookieContent=null;
		Integer userId=null;
		Cookie[] cookies=request.getCookies();
		if (cookies==null || cookies.length==0){
			return null;
		}
		for(Cookie cookie:cookies){
			if (cookie.getName().equalsIgnoreCase(BasicAppConstant._KEY_USER_PROFILE_COOKIE)){
				findUserProfileCookie = true;
				userProfileCookieContent = cookie.getValue();
				try{
					userId = Integer.parseInt(userProfileCookieContent);
				}catch(NumberFormatException nfEx){
					nfEx.printStackTrace();
					return null;
				}
				break;
			}
		}
		
		if (!findUserProfileCookie){
			return null;
		}

		return userId;
	}
	
	public static  void storeUserProfileInCookie(HttpServletResponse response,Integer userId){
		//TODO
		Cookie userProfileCookie = new Cookie(BasicAppConstant._KEY_USER_PROFILE_COOKIE,userId+"");
		userProfileCookie.setMaxAge(60*60*24*14);//2 weeks
		response.addCookie(userProfileCookie);
	}
	
	public static void removeUserProfileFromCookie(HttpServletResponse response){
		Cookie userProfileCookie = new Cookie(BasicAppConstant._KEY_USER_PROFILE_COOKIE,null);
		userProfileCookie.setMaxAge(0);
		response.addCookie(userProfileCookie);
	}
	
	public static boolean storeUserLoginRecorderInSession(HttpServletRequest request,BasicUserInfo userInfo){
		
		UserLoginRecorderVO loginRecorderVO = new UserLoginRecorderVO();
		
		loginRecorderVO.setUserInfo(userInfo);
		loginRecorderVO.setIpAddress(request.getRemoteAddr());
		loginRecorderVO.setLoginDate(System.currentTimeMillis());
		
		request.getSession().setAttribute(BasicAppConstant._KEY_USER_SESSION, loginRecorderVO);
		
		return true;
	}
	
	public static boolean removeUserLoginRecorderInSession(HttpServletRequest request){
		request.getSession().removeAttribute(BasicAppConstant._KEY_USER_SESSION);
		return true;
	}
	
	public static boolean storeAdminLoginRecorderInSession(HttpServletRequest request,BasicAdminInfo adminInfo,
			List<AdminRole> roles,List<AdminPrivilege> privileges){
		AdminLoginRecorderVO loginRecorderVO = new AdminLoginRecorderVO();
		loginRecorderVO.setAdminInfo(adminInfo);
		loginRecorderVO.setRoles(roles);
		loginRecorderVO.setPrivileges(privileges);
		loginRecorderVO.setIpAddress(request.getRemoteAddr());
		loginRecorderVO.setLoginDate(System.currentTimeMillis());
		request.getSession().setAttribute(BasicAppConstant._KEY_ADMIN_SESSION,loginRecorderVO);
		return true;
	}
	
	public static boolean removeAdminLoginRecorderInSession(HttpServletRequest request){
		request.getSession().removeAttribute(BasicAppConstant._KEY_ADMIN_SESSION);
		return true;
	}
	
}
